<template>
  <section class="h100 pt15 tabs-fixed">
    <van-tabs v-model="activeName" animated swipeable @change="onChange">
      <van-tab
          :title="item.title"
          :name="item.name"
          v-for="(item, index) in tabs"
          :key="index"
      >
        <van-row class="coupon-panel">
          <van-row class="title"></van-row>
          <van-row class="discount-panel">
            <div
                :class="['item', couponItem['isGet'] === 1 && 'disabled']"
                v-for="(couponItem, couponIndex) in couponListData"
                :key="couponIndex"
            >
              <van-row class="panel">
                <van-row class="top">
                  <van-row class="money">
                    <span>¥</span>{{ couponItem['moneyYuan'] }}
                  </van-row>
                  <van-row class="content">
                    <p>{{ couponItem['title'] }}</p>
                    <p>{{ couponItem['des'] }}</p>
                  </van-row>
                </van-row>
                <van-row class="date">{{ couponItem['validDes'] }}</van-row>
              </van-row>
              <van-row class="status">
                <van-row class="btns" v-if="couponItem['isGet'] === 2" @click="getCoupon(couponItem['id'])">
                  立即领取
                </van-row>
                <van-row class="btns" v-else>已领取</van-row>
              </van-row>
            </div>
          </van-row>
        </van-row>
      </van-tab>
    </van-tabs>
  </section>
</template>

<script>
import {BRStorage} from "../../../utils/brStorage";

export default {
  data() {
    return {
      activeName: "/coupon/list",
      tabs: [
        {
          title: "礼包",
          name: "/gift/list",
        },
        {
          title: "优惠券",
          name: "/coupon/list",
        },
        // {
        //   title: "活动",
        //   name: "/activities/list",
        // },
      ],
      couponListData: [],
    };
  },
  created() {
    this.getLoad();
  },
  methods: {
    //初始化加载
    getLoad() {
      let queryData = this.$route.query;
      let userToken = queryData.userToken;
      let fromNative = queryData.native;
      let gameId = queryData.gameId;
      let channelCode = queryData.channelCode;
      let runRomSystem = queryData.runRomSystem;
      this.sdkVersionCode = this.$route.query.sdkVersionCode;
      if (this.BRIsSetString(fromNative)) {
        BRStorage.setStorage(BRStorage.KeyUserToken(), userToken);
        BRStorage.setStorage(BRStorage.KeyGameID(), gameId);
        BRStorage.setStorage(BRStorage.KeyChannelCode(), channelCode);
        BRStorage.setStorage(BRStorage.KeyRunRomSystem(), runRomSystem);
        BRStorage.setStorage(BRStorage.KeyBRSDKVersionCode(), this.sdkVersionCode);
      } else {
        this.sdkVersionCode = BRStorage.getStorage(
            BRStorage.KeyBRSDKVersionCode()
        );
      }
      let params = {
        userToken: userToken,
      };
      this.$http(window.config.webHomeApiV2 + "/mb/coupon/list", params).then(
          (res) => {
            let code = res.code;
            if (code !== 1) {
              this.BRToast(res.msg);
              return;
            }
            this.couponListData = res.data;
          }
      );
    },

    //领取优惠券
    getCoupon(couponId) {
      let params = {
        couponId: couponId,
      };
      this.$http(window.config.webHomeApiV2 + "/mb/coupon/user_get", params).then(
          (res) => {
            let code = res.code;
            if (code !== 1) {
              this.BRToast(res.msg);
              return;
            }
            window.location.reload();
          }
      );
    },
    //tab切换
    onChange(e) {
      this.activeName = e;
      this.$router.push(e);
    },
  },
};
</script>

<style lang="less">
@import "../../../assets/css/welfare/index.less";
</style>
